package com.ywk.base.toolClass.hutool;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zbh
 * @Date 2023/12/8
 */
public class TreeUtilTest {

    static List<Tree<String>> tree=new ArrayList<>();

    public static void main(String[] args) {
        List<User> users = new ArrayList<>();
        users.add(new User("1", "", "0", "手机"));
        users.add(new User("2", "", "1", "小米"));
        users.add(new User("3", "", "1", "华为"));
        users.add(new User("4", "", "0", "水果"));
        users.add(new User("5", "", "4", "香蕉"));
        users.add(new User("6", "", "4", "菠萝"));
        users.add(new User("7", "", "4", "苹果"));
        users.add(new User("8", "", "4", "雪梨"));
        TreeNodeConfig config = new TreeNodeConfig();
        List<Tree<String>> treeList = TreeUtil.build(users, "0", config, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getPid());
            tree.setName(treeNode.getName());
            //扩展属性
            tree.putExtra("remark", treeNode.getName()+"-"+treeNode.getId());
        });
        tree=treeList;
        /*treeList.get(1).getChildren().remove(0);
        treeList.get(1).getChildren().remove(2-1);*/
        addInfo(treeList);
        System.out.println(JSONUtil.toJsonStr(treeList));

        System.out.println(JSONUtil.toJsonStr(findParentIds("2",tree)));
    }

    /**
     * 添加父节点id
     */
    public static void addInfo(List<Tree<String>> treeNodes){
        if (treeNodes!=null){
            for (Tree<String> treeNode : treeNodes) {
                //添加所有父节点id
                treeNode.putExtra("parentIds",findParentIds(treeNode));
                treeNode.putExtra("childrenIds",findChildIds(treeNode));
                addInfo(treeNode.getChildren());
            }
        }
    }

    /**
     * 通过节点查找所有父节点id
     * @param node
     * @return
     */
    public static List<String> findParentIds(Tree<String> node) {
        List<String> result = new ArrayList<>();
        Tree<String> parent = node.getParent();
        while (null != parent) {
            result.add(parent.getId());
            parent = parent.getParent();
        }
        return result;
    }

    /**
     * 通过id查找父节点id
     * @param id
     * @param treeNodes
     * @return
     */
    public static List<String> findParentIds(String id,List<Tree<String>> treeNodes) {
        List<String> result = new ArrayList<>();
        Tree<String> node = null;
        for (Tree<String> treeNode : treeNodes) {
            node = TreeUtil.getNode(treeNode, id);
            if (node!=null){
                break;
            }
        }
        Tree<String> parent = node.getParent();
        while (null != parent) {
            result.add(parent.getId());
            parent = parent.getParent();
        }
        return result;
    }

    /**
     * 查找所有子节点id
     * @param node
     * @return
     */
    public static List<String> findChildIds(Tree<String> node) {
        List<String> childIds = new ArrayList<>();
        childIds.add(node.getId());
        findChildIdsHelper(node, childIds);
        return childIds;
    }

    private static void findChildIdsHelper(Tree<String> node, List<String> childIds) {
        if (node.hasChild()) {
            for (Tree<String> child : node.getChildren()) {
                childIds.add(child.getId());
                findChildIdsHelper(child, childIds);
            }
        }
    }
}

@NoArgsConstructor
@AllArgsConstructor
@Data
class User {

    private String id;
    private String userId;
    private String pid;
    private String name;
}
